package com.kanavi.redisson.business.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.kanavi.redisson.constant.Const;
import com.kanavi.redisson.business.entity.BatchOrder;
import com.kanavi.redisson.business.service.BatchOrderService;
import com.kanavi.redisson.config.SnowflakeConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author tanghailan
 * @since 2021-03-28
 */
@RestController
@RequestMapping("/business/batchOrder")
public class BatchOrderController {
    @Autowired
    private BatchOrderService batchOrderService;

    @Autowired
    private SnowflakeConfig idWorker;

    static List<BatchOrder> orderList = new ArrayList<>();

    @PostConstruct
    public void init() {
        for (int i = 0; i < Const.NUM; i++) {
            BatchOrder order = new BatchOrder();
            order.setId(idWorker.snowflakeId())
                    .setOrderCode(RandomUtil.randomString(10)).
                    setOrderMoney(RandomUtil.randomBigDecimal()).
                    setOrderStatus(RandomUtil.randomBoolean())
                    .setOrderTime(DateUtil.date()).
                    setSerialNo(RandomUtil.randomString(5));
            orderList.add(order);

        }
    }

    @GetMapping("/batchInsert")
    public Integer batchInsert() {
        boolean b = batchOrderService.saveBatch(orderList);
        System.out.println(b);
        return orderList.size();
    }
}

